package com.gs.m_my_goods.persistent

import android.content.Context
import android.os.Handler
import android.os.Looper
import com.gs.common.BaseStateCallback
import com.gs.common.constant.StateCode
import com.gs.m_my_goods.model.MyGoodsModel

class MyGoodsPersistent(context: Context) {

    private val myGoodsModel by lazy { MyGoodsModel.getInstance(context) }
    private val handler = Handler(Looper.getMainLooper())

    fun getGoodsByUsername(username: String, callback: OnDoGetGoodsStateCallback) {
        Thread{
            myGoodsModel.getGoodsByUsername(username) {
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onGetGoodsLoading()
                        }
                    }
                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onGetGoodsSuccess()
                        }
                    }
                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onGetGoodsFailed()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }
                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }
                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }

    fun deleteGood(goodId: Int, callback: OnDeleteGoodStateCallback){
        Thread {
            myGoodsModel.deleteGood(goodId) {
                when (it) {
                    StateCode.STATE_LOADING -> {
                        handler.post {
                            callback.onDeleteGoodLoading()
                        }
                    }

                    StateCode.STATE_SUCCESS -> {
                        handler.post {
                            callback.onDeleteGoodSuccess()
                        }
                    }

                    StateCode.STATE_FAILED -> {
                        handler.post {
                            callback.onDeleteGoodFailed()
                        }
                    }
                    StateCode.NETWORK_ERROR -> {
                        handler.post {
                            callback.onNetworkError()
                        }
                    }

                    StateCode.SERVER_ERROR -> {
                        handler.post {
                            callback.onServerError()
                        }
                    }

                    StateCode.UNKNOWN_ERROR -> {
                        handler.post {
                            callback.onUnknownError()
                        }
                    }
                }
            }
        }.start()
    }


    interface OnDeleteGoodStateCallback: BaseStateCallback{
        fun onDeleteGoodLoading() {}
        fun onDeleteGoodSuccess()
        fun onDeleteGoodFailed()
    }


    interface OnDoGetGoodsStateCallback: BaseStateCallback{
        fun onGetGoodsLoading(){}
        fun onGetGoodsSuccess()
        fun onGetGoodsFailed()
    }

    companion object {
        private var instance: MyGoodsPersistent? = null
        fun getInstance(context: Context): MyGoodsPersistent {
            if (instance == null) {
                synchronized(this::class) {
                    if (instance == null) {
                        instance = MyGoodsPersistent(context)
                    }
                }
            }
            return instance!!
        }
    }
}